#include <iostream>
#include <cstring>
using namespace std;

void dfs(int p[101][101],int q[101][101],int pflag[101], int qflag[101],int n, int &maxCount, int Count, int now){
	if(Count > n) return;
	if(Count == n){
		if(now > maxCount) maxCount = now;
		return;
	}
	for(int i = 1; i <= n; i++){
		if(pflag[i] == 1) continue;
		pflag[i] = 1;
		for(int j = 1; j <= n; j++){
			if(qflag[j] == 1) continue;
			qflag[j] = 1;
			dfs(p, q, pflag, qflag, n, maxCount, Count + 1, now + p[i][j] * q[j][i]);
			qflag[j] = 0;
		}
		pflag[i] = 0;
	}
}

int findBest(int p[101][101], int q[101][101], int n){
	int pflag[101];
	int qflag[101];
	memset(pflag, 0, sizeof(pflag));
	memset(qflag, 0, sizeof(qflag));
	int maxCount = 0;
	dfs(p, q, pflag, qflag, n, maxCount, 0, 0);
	return maxCount;
}

int main(){
	int n;
	cin >> n;
	int p[101][101];
	int q[101][101];
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			cin >> p[i][j];
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			cin >> q[i][j];
	int max = findBest(p,q,n);
	cout << max << endl;
}
